package com.yxt.ui;

import java.util.ArrayList;
import java.util.List;

import com.yxt.common.sys.CommConstant;
import com.yxt.common.sys.StringUtil;
import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.ui.NormalTableAdapter;
import com.yxt.common.ui.NormalTableAdapter.TableCell;
import com.yxt.common.ui.NormalTableAdapter.TableRow;
import com.yxt.model.Station;
import com.yxt.service.StationService;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 测站查询
 * @author Crazyman
 */
public class StationSearchResultActivity extends Activity {
	private int cols = 2;
	private int height = CommConstant.screenHeight / 10;
	private int color = Color.WHITE;
	private EditText txtValue;	
	private String type;
	private String value;
	private TextView lblTitle;
	private ListView lstWaterStation;
	private StationService service = new StationService(StationSearchResultActivity.this);
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.station_result); // 软件activity的布局
		ActivityStackControlUtil.add(this);
		initComponent();
	}

	private void initComponent() {
		Button btnBackHome = (Button) findViewById(R.id.btnBackHome);		
		btnBackHome.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		Button btnClear = (Button) findViewById(R.id.btnClear);		
		btnClear.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				txtValue.setText("");
			}
		});
		lblTitle = (TextView)findViewById(R.id.lblTitle);
		lstWaterStation = (ListView) findViewById(R.id.lstStationList);
		Intent intent = getIntent();
		type = intent.getStringExtra("type");
		value = intent.getStringExtra("value");
		txtValue = (EditText) findViewById(R.id.txtValue);
		txtValue.setText(value);
		txtValue.addTextChangedListener(new TextWatcher(){             
            public void afterTextChanged(Editable s) { 
            } 

            public void beforeTextChanged(CharSequence s, int start, int count, 
                    int after) { 
            } 
 
            public void onTextChanged(CharSequence s, int start, int before, 
                    int count) { 
            	value = s.toString();
            	if (StringUtil.isNotEmpty(value)) {
                	fillStationList(service.getStationListByCondition(type, value));
            	}
            } 
        });
		fillStationList(service.getStationListByCondition(type, value));
	}
	
	private void fillStationList(List<Station> list) {
		List<TableRow> table = new ArrayList<TableRow>();
		int[] width = { 250, 50 };
		int size = list.size();
		for (int i = 0; i < size; i++) {
			// 每行的数据
			TableCell[] cells = new TableCell[cols];
			int len = cells.length;
			Station station = list.get(i);
			String type = station.getType();
			String header = CommConstant.STATIONTYPE_MAP.get(type);
			if (header == null) {
				header = CommConstant.StationType.TYPE_HD;
			}
			for (int j = 0; j < len; j++) {
				if (j == len - 1) {
					cells[j] = new TableCell(R.drawable.expand, width[j], height, color, TableCell.IMAGE, new TextItemOnClickListener(station));
				} else {
					cells[j] = new TableCell(header + "■" + station.getName() + "@" + station.getAddr(), width[j], height, color, TableCell.DEFINE);
				}
			}
			table.add(new TableRow(cells));
		}
		NormalTableAdapter tableAdapter = new NormalTableAdapter(this, table);		
		lstWaterStation.setAdapter(tableAdapter);
		lblTitle.setText(getString(R.string.station_count) + "(" + String.valueOf(size) + ")");
	}

	private class TextItemOnClickListener implements OnClickListener {
		private Station station;
		
		public TextItemOnClickListener(Station station) {
			this.station = station;
		}


		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("code", station.getCode());
			intent.putExtra("name", station.getName());
			int type = Integer.parseInt(station.getType());
			switch(type){
				case CommConstant.Station.RAIN_LOCATION:
					intent.setClass(getApplicationContext(),RainStationDetailActivity.class);
					break;
				case CommConstant.Station.WATER_LOCATION:
					intent.setClass(getApplicationContext(),WaterStationDetailActivity.class);
					break;
				case CommConstant.Station.SOIL_LOCATION:
					intent.setClass(getApplicationContext(),SoilStationDetailActivity.class);
					break;
				case CommConstant.Station.SALT_LOCATION:
					intent.setClass(getApplicationContext(),SaltStationDetailActivity.class);
					break;
			}
			startActivity(intent);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
        ActivityStackControlUtil.remove(this);
	}
}